package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/role")
public class RoleController {


    @Reference
    RoleService roleService;

    @RequestMapping("/findRole")
    public Result findRole(){

        log.debug("role============获得信息!!!!");
        List<Role> role = roleService.findRole();
//        Object[] roles = role.toArray();
        log.debug("role============"+role);

        return new Result(true,"查询成功",role);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionsIds,Integer[] menusIds){
        log.debug("新增role:"+role);
        log.debug("新增perssionItemIds:"+permissionsIds);
        log.debug("新增menuItemIds:"+menusIds);
        roleService.add(role,permissionsIds,menusIds);
        log.debug("新增角色成功");
        return new Result(true, MessageConst.ADD_ROLE_SUCCESS);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionsIds,Integer[] menusIds){
        log.debug("编辑role："+role);
        log.debug("perssionItemIds："+permissionsIds);
        log.debug("编辑rwmenuItemIdsole："+menusIds);

        roleService.edit(role,permissionsIds,menusIds);

        return  new Result(true,MessageConst.EDIT_ROLE_SUCCESS);

    }

    /**
     * 查找角色基本数据
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("RolecontrollerfindById:"+id);

        Role role= roleService.findById(id);
        log.debug("rotle:"+role);
        return new Result(true,MessageConst.ACTION_SUCCESS,role);
    }



    @RequestMapping("/findPermissionById")
    public Result findPermissionById(Integer id){
        log.debug("findPermissionById:"+id);
        List<Integer> permissions =roleService.findPermissionById(id);

        log.debug("List<Permission> permissions:"+permissions);

        return new Result(true,MessageConst.ACTION_SUCCESS,permissions);
    }

    @RequestMapping("/findMenuById")
    public Result findMenuById(Integer id){
        log.debug("findMenuById:"+id);
        List<Integer> menus =roleService.findMenuById(id);
        log.debug("List<Menu> menus:"+menus);

        return new Result(true,MessageConst.ACTION_SUCCESS,menus);
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("controller{"+queryPageBean+"}");
        PageResult pageResult = roleService.findPage(queryPageBean);
        log.debug("角色分页查询成功!!");
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, pageResult);
    }

    @RequestMapping("/delById")
    public Result delById(Integer id) {
        log.debug("delById:" + id);
        roleService.delById(id);
        log.debug("删除角色成功!!");
        return new Result(true, MessageConst.DELETE_ROLE_SUCCESS);
    }

}
